//
//  App.swift
//  Duyiguqin
//
//  Created by 张昭 on 2024/7/29.
//

import UIKit
/*
 
发送短信：
http://tiaoyin.dyxt.org/du1/system/account/sms/send
GET请求
参数 phonenumber

登录
http://tiaoyin.dyxt.org/du1/system/account/register
POST
参数json
{
    "phonenumber": "",
    "smsCode": "0369"
}

http://tiaoyin.dyxt.org/du1/system/music/list/all - 查询所有
http://tiaoyin.dyxt.org/du1/system/music/list  - 分页查询

http://tiaoyin.dyxt.org/du1/system/aboutInfo/1 - “关于我们”信息接口
http://tiaoyin.dyxt.org/du1/system/music/getStartupInfo - 开机图片

 */

import AVFoundation

class App {
    
    static let kHomeURL = URL(string: "http://www.dyxt.org/9duyiqinke/")!
    
    static let kHomeRequestURL = URL(string: "http://tiaoyin.dyxt.net/du1/system/music/getBannerInfo")!
    
    /// 关于我们
    static let kAboutURL = URL(string: "http://tiaoyin.dyxt.org/du1/system/aboutInfo/1")!
    
    static let kAllMusic = URL(string: "http://tiaoyin.dyxt.org/du1/system/music/list/all")!
    
    static let kSmsSend = URL(string: "http://tiaoyin.dyxt.org/du1/system/account/sms/send")!
    
    static let kRegister = URL(string: "http://tiaoyin.dyxt.org/du1/system/account/register")!

    static let shared = App()

    var aboutInfo: String?
    var officialInfo: String?
    var contactInfo: String?
    var attributedItems: [String: NSAttributedString] = [:]
    var cachedMusics: [URL: URL] = [:]
    
    var userName: String? {
        set {
            UserDefaults.standard.setValue(newValue, forKey: "USERNAME")
            UserDefaults.standard.synchronize()
        }
        get {
            UserDefaults.standard.object(forKey: "USERNAME") as? String ?? nil
        }
    }
    var userAvatar: String?
    
    func clean() {
        aboutInfo = nil
        officialInfo = nil
        contactInfo = nil
        attributedItems = [:]
    }
}

enum Tone: Equatable {
    case F
    case bB
    case C
    case G
    case bE
    case D
    
    var shortTitle: String {
        switch self {
        case .F:
            return "F调（正调）"
        case .bB:
            return "bB调（紧五弦）"
        case .C:
            return "C调（慢三弦）"
        case .G:
            return "G调（慢一、三、六弦）"
        case .bE:
            return "bE调（紧二、五、七弦"
        case .D:
            return "D调（慢一、三、四、六弦"
        }
    }
    
    var title: String {
        switch self {
        case .F:
            return "F调（正调）正宫调 黄钟调"
        case .bB:
            return "bB调（紧五弦）蕤宾调"
        case .C:
            return "C调（慢三弦）慢角调"
        case .G:
            return "G调（慢一、三、六弦）慢宫调"
        case .bE:
            return "bE调（紧二、五、七弦）清商调"
        case .D:
            return "D调（慢一、三、四、六弦）商调"
        }
    }
    
    var desc: String {
        switch self {
        case .F:
            return "C、D、F、G、A、c、d，唱作5、6、1、2、3、5、6"
        case .bB:
            return "C、D、F、G、bB、c、d，唱作2、3、5、6、1、2、3"
        case .C:
            return "C、D、E、G、A、c、d，唱作1、2、3、5、6、1、2"
        case .G:
            return "B、D、E、G、A、B、d，唱作3、5、6、1、2、3、5"
        case .bE:
            return "C、bE、F、G、bB、c、be，唱作6、1、2、3、5、6、1"
        case .D:
            return "B、D、E、F、A、b、d，唱作6、1、2、3、5、6、1"
        }
    }
    
    
    var pitchs: [(String, Double)] {
        switch self {
        case .F:
            return [("C", 0.148659), ("D", 0.166863), ("F", 0.19843), ("G", 0.22272), ("A", 0.25), ("c", 0.297295), ("d", 0.333704)]
        case .bB:
            return [("C", 0.148659), ("D", 0.166863), ("F", 0.198431), ("G", 0.222727), ("bB", 0.264863), ("c", 0.29729), ("d", 0.333704)]
        case .C:
            return [("C", 0.148659), ("D", 0.166863), ("E", 0.187295), ("G", 0.222727), ("A", 0.25), ("c", 0.297295), ("d", 0.333704)]
        case .G:
            return [("B", 0.1403182), ("D", 0.166863), ("E", 0.187295), ("G", 0.222727), ("A", 0.25), ("B", 0.2806136), ("d", 0.333704)]
        case .bE:
            return [("C", 0.148659), ("bE", 0.176772), ("F", 0.198431), ("G", 0.222727), ("bB", 0.264863), ("c", 0.297295), ("be", 0.35354545)]
        case .D:
            return [("B", 0.1403182), ("D", 0.166863), ("E", 0.187295), ("F", 0.210227273), ("A", 0.25), ("b", 0.2806136), ("d", 0.333704)]
        }
    }
}

struct Instrument {
    let id: String
    let name: String
    let url: URL
}
